package com.zyw.net.http;


import com.alibaba.druid.support.json.JSONUtils;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * HttpKitUtils request params
 *
 * @author vill
 */
public class HttpParams {
    /**
     * 请求参数
     */
    private Map<String, String> requestData;
    private Map<String, String> requestHeader;
    private Map<String, Object> requestConf;

    /**
     * http body
     */
    private Object httpBody;

    /**
     * 存储 扩展属性
     */
    private Properties properties;

    private HttpParams() {
    }

    public static HttpParams create() {
        return new HttpParams();
    }

    /**
     * 添加请求头
     *
     * @param key
     * @param value
     * @return
     */
    public HttpParams addHeader(String key, String value) {
        if (requestHeader == null) requestHeader = new LinkedHashMap<>();
        requestHeader.put(key, value);
        return this;
    }

    /**
     * 添加参数
     *
     * @param key
     * @param value
     * @return
     */
    public HttpParams addParameter(String key, String value) {
        if (requestData == null) requestData = new LinkedHashMap<>();
        requestData.put(key, value);
        return this;
    }

    public HttpParams setConnectTimeout(int connectTimeout) {
        if (requestConf == null) requestConf = new LinkedHashMap<>();
        requestConf.put("connectTimeout", connectTimeout);
        return this;
    }

    public HttpParams setReadTimeout(int readTimeout) {
        if (requestConf == null) requestConf = new LinkedHashMap<>();
        requestConf.put("readTimeout", readTimeout);
        return this;
    }

    public HttpParams setProperty(String key, String value) {
        if (properties == null) properties = new Properties();
        properties.setProperty(key, value);
        return this;
    }

    public String getProperty(String key) {
        if (key == null || properties == null) return null;
        return properties.getProperty(key);
    }

    public Map<String, String> getRequestData() {
        return requestData;
    }

    public Map<String, String> getRequestHeader() {
        return requestHeader;
    }

    public Map<String, Object> getRequestConf() {
        return requestConf;
    }

    public void setRequestConf(Map<String, Object> requestConf) {
        this.requestConf = requestConf;
    }

    public void setRequestParams(Map<String, String> requestParams) {
        if (this.requestData == null) {
            this.requestData = new LinkedHashMap<>();
        }
        this.requestData.putAll(requestParams);
    }

    public void setHttpBody(Object body) {
        this.httpBody = body;
    }

    public Object getHttpBody() {
        return httpBody;
    }

    @Override
    public String toString() {
        if (requestData != null) return JSONUtils.toJSONString(requestData);
        if (httpBody != null) return JSONUtils.toJSONString(httpBody);
        return "no params";
    }
}
